package com.lc.ibps.platform.rest.org;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.org.service.IPartyGroupMgrService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 用户组控制器，第三方系统调用
 *
 * <pre> 
 * 构建组：ibps-platform-webapi
 * 作者：Lium
 * 邮箱：1316679699@qq.com
 * 日期：2017年7月18日-下午8:36:36
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value="/api/webapi/groupMgrService")
@Api(value = "/groupMgrService",tags="用户管理-用户组操作服务")
@RestController
public class GroupMgrController extends BaseApiController{
	
	private IPartyGroupMgrService groupMgrService;
	
	public GroupMgrController(){
		groupMgrService = AppUtil.getBean(IPartyGroupMgrService.class);
	}
	
	/**
	 * 添加用户组信息
	 *
	 * @param json {id:""/*选填/,name:"group1"/*必填/,groupAlias:"用户组1"/*必填/,groupNote:"用户组描述"/*选填/,
					partyUserGroupPoList:[{id:"",userId:"498b9030",userName:"test",groupId:""}]/*选填/}";
	 */
	@ApiOperation(value = "添加用户组信息", notes = "添加用户组信息")
	@RequestMapping(value="/createGroup",method=RequestMethod.PUT)
	public APIResult<String> createGroup(
			@ApiParam(value = "用户组信息json", required = true)
			@RequestParam(name = "json", required = false) String json
			){	
		APIResult<String> result = new APIResult<>();
		try{
			groupMgrService.createGroup(json);	 //TODO 测试报错		
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加用户组信息失败！", e);
		}
		return result;
	}
	
	/**
	 * 修改用户组信息
	 *
	 * @param json {id:"33687884",name:"group1"/*必填/,groupAlias:"用户组1"/*必填/,groupNote:"用户组描述"/*选填/,
			partyUserGroupPoList:[{id:"39635968",userId:"498b9030",userName:"test",groupId:"33687884"},
									{id:"",userId:"498b9030",userName:"test",groupId:""]/*选填/}";
	 */
	@ApiOperation(value = "修改用户组信息", notes = "修改用户组信息")
	@RequestMapping(value="/updateGroup",method=RequestMethod.POST)
	public APIResult<String> updateGroup(
			@ApiParam(value = "用户组信息json", required = true)
			@RequestParam(name = "json", required = true) String json
			){
		APIResult<String> result = new APIResult<>();
		try{
			groupMgrService.updateGroup(json);			//TODO 测试报错
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "修改用户组信息失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "删除用户组信息", notes = "根据组ID删除用户组信息")
	@RequestMapping(value="/deleteGroup",method=RequestMethod.DELETE)
	public APIResult<String> deleteGroup(
			@ApiParam(value = "组ID", required = true)
			@RequestParam(name = "id", required = true) String id
			) {
		APIResult<String> result = new APIResult<>();
		try{
			groupMgrService.deleteGroup(id);			
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "删除用户组信息失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "添加用户组用户", notes = "根据组ID添加用户组用户")
	@RequestMapping(value="/addEmployee2Group",method=RequestMethod.PUT)
	public APIResult<String> addEmployee2Group(
			@ApiParam(value = "组ID", required = true)
			@RequestParam(name = "groupId", required = true) String groupId,
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "userId", required = true) String userId
			) {
		APIResult<String> result = new APIResult<>();
		try{
			groupMgrService.addEmployee2Group(groupId, userId);			
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加用户组用户失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "移除用户组用户", notes = "根据用户ID移除用户组")
	@RequestMapping(value="/removeEmployee4Group",method=RequestMethod.DELETE)
	public APIResult<String> removeEmployee4Group(
			@ApiParam(value = "组ID", required = true)
			@RequestParam(name = "groupId", required = true) String groupId,
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "userId", required = true) String userId
			) {	
		APIResult<String> result = new APIResult<>();
		try{
			groupMgrService.removeEmployee4Group(groupId, userId);		
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "移除用户组用户失败！", e);
		}
		return result;
	}
}
